package com.welsy.user.command
{
	import mx.rpc.IResponder;
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;	
	import com.welsy.user.model.UserModelLocator;
	import com.welsy.util.Comparator;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.events.FaultEvent;
	import mx.controls.Alert;
	import mx.core.Application;
	import com.welsy.user.event.GetUserCreditsEvent;
	import com.welsy.util.NamespaceLocator;
	import com.welsy.user.business.GetUserCreditsDelegate;
	import mx.collections.ArrayCollection;		
	
	/**
	 * @version	$Revision: 1.1 $
	 */
	public class GetUserCreditsCommand implements ICommand, IResponder
	{
	
		public function execute( event : CairngormEvent ): void
		{		
			var guc_eve : GetUserCreditsEvent = GetUserCreditsEvent(event);			
			var delegate : GetUserCreditsDelegate = new GetUserCreditsDelegate( this );
			delegate.getCredits(guc_eve.userId);					    			
		}
	
		public function result( event : Object ) : void
		{				
			var umodel : UserModelLocator = UserModelLocator.getInstance();
			
			var xres : XMLList = new XMLList(event.result);
			
			var NS : Namespace = new Namespace(NamespaceLocator.USER_RES_DATATYPES);
			var TAG_GETUSERCREDITSRESULTS : QName = new QName(NS, "GetUserCreditsResult");
			var TAG_TOTAL : QName = new QName(NS, "Total");
			var TAG_ACHIEVED : QName = new QName(NS, "Achieved");
			
			var t_gucr : XMLList = xres.descendants(TAG_GETUSERCREDITSRESULTS);
			var v_tot : int = int(t_gucr.child(TAG_TOTAL));
			var v_ach : int = int(t_gucr.child(TAG_ACHIEVED));
			
			//Update the model
			umodel.graphUserStat = new ArrayCollection( [
            { Label: "Completati", Courses: v_ach },
            { Label: "Rimanenti", Courses: v_tot-v_ach }
            ]);
            umodel.graphView = umodel.graphUserStat;
		}
				
		public function fault( event : Object ) : void
		{
			Alert.show("GetUserCreditsCommand.fault()!" );
		}
	}

}